package com.alks.function.service.impl.pcfactoryquery;

import com.alks.common.aop.AutoPageAop;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.beanutils.BeanUtil;
import com.alks.entity.data.entity.PcProdStatus2;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.dto.PcLotHeadCountDto;
import com.alks.function.data.dto.pcfactoryquery.PcMtlByLotDetailDto;
import com.alks.function.data.dto.pcfactoryquery.PcMtlByLotHeadDto;
import com.alks.function.data.dto.pcfactoryquery.PcSlipByLotDto;
import com.alks.function.data.request.chengKong.PcLotHeadRequest;
import com.alks.function.data.request.factoryquery.SpMtlDetailRequest;
import com.alks.function.data.request.factoryquery.SpMtlHeadRequest;
import com.alks.function.data.request.factoryquery.SpUpProcessRequest;
import com.alks.function.mapper.PcProdStatus2Mapper;
import com.alks.function.mapper.chengKong.PcLotHeadMapper;
import com.alks.function.mapper.pcfactoryquery.FT_QR_PcMtlQTempMapper;
import com.alks.function.service.pcfactoryquery.IPcMtlSupportService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.alks.entity.data.enums.ResultCodeEnum.SUCCESS;

@Service
@Slf4j
public class PcMtlSupportServiceImpl implements IPcMtlSupportService {
    @Autowired
    PcLotHeadMapper headMapper;
    @Autowired
    FT_QR_PcMtlQTempMapper tempMapper;
    @Autowired
    PcProdStatus2Mapper status2Mapper;

    @Override
    @AutoPageAop
    public Map<String, Object> queryMtlHead(SpMtlHeadRequest request) {
        /*分页查询基础数据*/
        List<PcMtlByLotHeadDto> dtos = headMapper.getMtlHead(request);
        /*入参拷贝*/
        PcLotHeadRequest headRequest = BeanUtil.copyBean(request, PcLotHeadRequest.class);
        /*查询预补和订单和*/
        List<PcLotHeadCountDto> count = headMapper.count(headRequest);
        /*封装输出结果*/
        Map<String, Object> map = new HashMap<>(3);
        if (request.getPageNum() != -1 && request.getPageSize() != -1) {
            Page<PcMtlByLotHeadDto> page = (Page<PcMtlByLotHeadDto>) dtos;
            map.put("total", page.getTotal());
        }
        map.put("sum", count);
        map.put("mtlhead", dtos);
        return map;
    }

    @Override
    @AutoPageAop
    public Map<String, Object> queryMtlDetail(SpMtlDetailRequest request) {
        String companyId = UserIdThread.get().getCompanyId();
        /*获取数据*/
        List<PcMtlByLotDetailDto> detailDto = tempMapper.queryMtlDetail(request,companyId);
        /*统计用量和*/
        Double count = tempMapper.countUsageTtl(request);
        /*封装输出结果*/
        Map<String,Object> map = new HashMap<>(3);
        if (request.getPageNum() != -1 && request.getPageSize() != -1) {
            Page<PcMtlByLotDetailDto> page = (Page<PcMtlByLotDetailDto>) detailDto;
            map.put("total", page.getTotal());
        }
        map.put("detail", detailDto);
        map.put("count", count);
        return map;
    }

    @Override
    public ResponseInfo queryMtlAttribute(String lotNo) {
        /*查找工艺细节*/
//        PcMtlAttributeDto mtlDtos=headMapper.querySupportByLotNo(lotNo);
        /*查找补料数据*/
        List<PcSlipByLotDto> slipDto = tempMapper.querySlipByLotNo(lotNo);
        /*封装*/
        Map map = new HashMap();
//        map.put("processes",mtlDtos);
        map.put("slip", slipDto);
        return ResponseInfo.ok(map);
    }

    @Override
    public ResponseInfo updateProcess(SpUpProcessRequest request) {
        /*拷贝提取*/
        PcProdStatus2 status2 = BeanUtil.copyBean(request, PcProdStatus2.class);
        /*赋值*/
        LambdaUpdateWrapper<PcProdStatus2> update = new LambdaUpdateWrapper();
        update.eq(PcProdStatus2::getLotNo, request.getLotNo());
        status2Mapper.update(status2, update);
        /*返回*/
        return ResponseInfo.ok(SUCCESS.getMessage());
    }

    @Override
    public ResponseInfo queryVendor() {
        List<String> vendor = headMapper.queryVendor();
        return ResponseInfo.ok(vendor);
    }
}
